<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center">首页</div>
    </nav-bar>
    <tab-control class="tab-control" ref="tabControl1" :titles="['流行','新款','精选']" @tabClick="tabClick"
                 v-show="isTabFixed"></tab-control>
    <scroll class="content" ref="scroll" :probe-type="3" @scroll="contentScroll" :pull-up-load="true"
            @pullingUp="LoadMore" :pull-down="true">
      <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad"></home-swiper>
      <recommend-view :recommends="recommends"></recommend-view>
      <feature-view></feature-view>
      <tab-control ref="tabControl2" :titles="['流行','新款','精选']" @tabClick="tabClick"
                   :class="{fixed: isTabFixed}"></tab-control>
      <goods-list :goods="goods[currentType].list"></goods-list>
    </scroll>
    <back-top @click.native="backTopClick" v-show="isShowBackTop"></back-top>
  </div>
</template>

<script>
import NavBar from "@/components/common/navbar/NavBar";
import Scroll from "@/components/common/scroll/Scroll";
import GoodsList from "@/components/content/goods/GoodsList";
import BackTop from "@/components/content/backTop/BackTop";

import HomeSwiper from "@/views/home/childComps/HomeSwiper";
import RecommendView from "@/views/home/childComps/RecommendView";
import FeatureView from "@/views/home/childComps/FeatureView";
import TabControl from "@/components/content/tabControl/TabControl";
import {getHomeMultidata, getHomeGoods} from "@/network/home";
import {POP, NEW, SELL, BACKTOP_DISTANCE} from "@/common/const";
import {backTopMixin, itemListenerMixin} from "@/common/mixin";

export default {
  name: "Home",
  components: {
    NavBar,
    HomeSwiper,
    RecommendView,
    FeatureView,
    TabControl,
    GoodsList,
    Scroll,
    BackTop,
  },
  data() {
    return {
      banners: [],//轮播图
      recommends: [],//推荐
      goods: {
        'pop': {page: 0, list: []},//流行
        'new': {page: 0, list: []},//新款
        'sell': {page: 0, list: []},//精选
      },
      currentType: POP,
      tabOffsetTop: 0,//tabControll距离顶部的距离
      isTabFixed: false,//tabControll 是否吸顶
      saveY: 0,//离开页面时y轴的坐标
    }
  },
  created() {
    this.getHomeMultidata();
    this.getHomeGoods(POP);
    this.getHomeGoods(NEW);
    this.getHomeGoods(SELL);
  },

  //使用混入，所以注释mounted()
  mixins: [itemListenerMixin, backTopMixin],
  /*mounted() {
    //监听item中图片加载完成
    const refresh = debounce(this.$refs.scroll.refresh, 200)
    this.itemImageLoad = () => {//重新计算可滚动区域
      refresh();
    }
    this.$bus.$on('itemImageLoad', this.itemImageLoad);
  },*/
  activated() {
    //页面活跃时跳转到当前视图y坐标
    this.$refs.scroll.refresh();
    this.$refs.scroll.scrollTo(0, this.saveY, 0);
  },
  deactivated() {
    //离开页面时保存当前视图y坐标
    this.saveY = this.$refs.scroll.getScrolly();
    //离开页面时取消全局事件的监听
    this.$bus.$off('itemImageLoad', this.itemImageLoad)
  },
  methods: {
    /**
     * 事件监听相关的方法
     */
    //切换首页展示的商品类型
    tabClick(index) {
      switch (index) {
        case 0:
          this.currentType = 'pop';
          break;
        case 1:
          this.currentType = 'new';
          break;
        case 2:
          this.currentType = 'sell';
          break;
      }
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index;

    },
    //监听Scroll的页面滚动事件,
    contentScroll(position) {
      //监听回到顶部按钮是否显示
      this.listenBackTopIsShow(position);

      //决定tabControll是否吸顶（position:fixed）
      this.isTabFixed = -position.y >= (this.tabOffsetTop);
    },
    //上拉加载更多
    LoadMore() {
      this.getHomeGoods(this.currentType);
      this.$refs.scroll.finishPullUp();
    },
    //监听轮播图加载完事件
    swiperImageLoad() {
      //获取tab-control的offsetTop
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop;
    },

    /**
     * 网络请求相关的方法
     */
    //加载轮播图和推荐
    getHomeMultidata() {
      getHomeMultidata().then(res => {
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
      })
    }
    ,
    //加载商品数据，新款、流行、精选
    getHomeGoods(type) {
      getHomeGoods(type, this.goods[type].page + 1).then(res => {
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page += 1;
      });
    }
  }
}
</script>

<style scoped>
#home {
  /* padding-top: 40px;*/
  height: 100vh;
  position: relative;
}

.home-nav {
  background-color: var(--color-tint);
  color: #fff;
  /*position: fixed;
  left: 0;
  top: 0;
  right: 0;
  z-index: 9;*/
}

.tab-control {
  position: relative;
  /*position: sticky;
  top: 44px; *//*元素滚动到某个位置时,sticky树型会自动改为flxed*/
  z-index: 9; /*优先显示等级*/
}

.content {
  overflow: hidden;
  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
}

/*.content {
  height: calc(100% - 93px);
  overflow: hidden;
  margin-top: 44px;
}*/

.fixed {
  position: fixed;
  left: 0;
  right: 0;
  top: 44px;
}
</style>
